/**
 * @Author: 罗显
 * @CreateTime: 2025-04-30
 * @Version: 1.0
 */


package com.it.user.service.impl;

import com.it.user.entity.UserEntity;
import com.it.user.mapper.UserMapper;
import com.it.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    //登录
    @Override
    public UserEntity login(String username, String password) {
        return userMapper.login(username, password);
    }

    //根据用户名查询
    @Override
    public UserEntity findUserByUsername(String username) {
        return userMapper.findUserByUsername(username);
    }

    //注册
    @Override
    public UserEntity register(UserEntity userEntity) {
        userMapper.register(userEntity);
        return userEntity;  //注册成功之后将UserEntity返回
    }

    //更新用户状态
    @Override
    public void lockUserById(Long id, UserEntity.UserStatus userStatus) {
        userMapper.updateStatus(id, userStatus.getCode()); //  使用枚举的 code 值进行更新
    }

    @Override
    public UserEntity findUserById(Long id) {
        return userMapper.findUserById(id);
    }

    @Override
    public boolean updateUserById(UserEntity userEntity) {
        int rowsAffected = userMapper.updateUserById(userEntity);
        return rowsAffected > 0; // 如果更新的行数大于0，则返回true，表示更新成功
    }

    @Override
    public boolean updatePassword(Long id, String password) {
        int rowsAffected =  userMapper.updatePassword(id, password);
        return rowsAffected > 0; //如果更新的行数大于0，则返回true，表示更新成功
    }

}